<%@ page import="java.util.*,com.db.tracker.vo.*,java.util.*,com.db.tracker.util.*,java.text.*,java.io.*"%>
<%@ page import="com.db.tracker.vo.ReleaseVO"%>
<%@ page session = "true" %>
<%try {
				
				session = request.getSession();



 			if (session.getAttribute("USER_ID") == null || !((String) session.getAttribute("SESSION_ID")).equals(session.getId())) {

					try {
						response.sendRedirect("LoginPage.jsp");
					} catch (IOException ioe) {
						ioe.printStackTrace();
					}

				} else { 
					
					
					String userid = (String)session.getAttribute("USER_ID")==null?"":(String)session.getAttribute("USER_ID");

					Properties property1 = new Properties();
					File propfile1 = new File(getServletContext().getInitParameter("tracker.properties.home")+"/"+"STtracker.properties");
					InputStream in1 = new FileInputStream(propfile1);
					property1.load(in1);
					String defaultunixserver;
					defaultunixserver = property1.getProperty("ReleaseUnixServer");	
				

					DBUtil dbutil = new DBUtil(getServletContext());
					UnixUtil unixutil = new UnixUtil(getServletContext());
					
					
					Vector relNameVector = new Vector();
					relNameVector = (Vector)session.getAttribute("ALL-REL-NAME-VECTOR");
					
					if(relNameVector == null){
						relNameVector = dbutil.getAllReleaseName();
						if(relNameVector.size()>0){
							session.removeAttribute("ALL-REL-NAME-VECTOR");
							session.setAttribute("ALL-REL-NAME-VECTOR",relNameVector);
						}
					}
					

					Vector logFileVector = new Vector();
					String releaseName = (String)request.getParameter("releaseName")==null?(request.getParameter("hiddenRelName")==null?"":request.getParameter("hiddenRelName")):request.getParameter("releaseName");
					String getLogButton = (String)request.getParameter("getLogButton")==null?"":request.getParameter("getLogButton");
	
				    String downloadlogfilebutton = (String)request.getParameter("downloadlogfilebutton")==null?"":request.getParameter("downloadlogfilebutton");

					String downloadlogfile = (String)request.getParameter("downloadlogfile")==null?"":request.getParameter("downloadlogfile");

					  if(downloadlogfilebutton.equals("Get File")){
	 						
							Properties property = new Properties();
							File propfile = new File(getServletContext().getInitParameter("tracker.properties.home")+"/"+"STtracker.properties");
							InputStream in = new FileInputStream(propfile);
							property.load(in);

							String logFilePath = property.getProperty("ReleaseParentDirLogPath");
							String logUnixServer = property.getProperty("ReleaseLogUnixServer");
							String logUnixUser = property.getProperty("ReleaseLogUnixUser");
							String logUnixPwd = property.getProperty("ReleaseLogUnixPwd");

							response.setContentType("application");
							response.setHeader("Content-disposition","attachment; filename=" + downloadlogfile );
  							System.out.println(">>>> user to download"+logUnixUser);
							String logdirpath = logFilePath+"/"+releaseName;
							System.out.println("while downloading file"+logdirpath);
							unixutil.downLoadFile( logUnixServer, logUnixUser, logUnixPwd, logFilePath, downloadlogfile, response.getOutputStream());
							response.flushBuffer();
							//	response.sendRedirect("/sl/reports/downLoadFileToSys.jsp");
							//	return;
						}
					 
						if(getLogButton.equals("Get Log")){
							
							Properties property = new Properties();
							File propfile = new File(getServletContext().getInitParameter("tracker.properties.home")+"/"+"STtracker.properties");
							InputStream in = new FileInputStream(propfile);
							property.load(in);
							
							String logFilePath = property.getProperty("ReleaseParentDirLogPath");
							String logUnixServer = property.getProperty("ReleaseLogUnixServer");
							String logUnixUser = property.getProperty("ReleaseLogUnixUser");
							String logUnixPwd = property.getProperty("ReleaseLogUnixPwd");	
							
							// logFilePath = logFilePath + "/" + releaseName;
							logFileVector = unixutil.getFilesInfo(logUnixServer, logUnixUser, logUnixPwd, logFilePath);
						
						}
					%>
<html>
	<head>
		<link type="text/css" rel="stylesheet" href="styles/tracker_style.css">
		<title>
			Release Logs
		</title>
		<script>

			function checkForRelease(obj){
			
			 if( obj.value == 0){
					alert("Kindly select Release Name from the list.");
					obj.focus();
					return false;
				 }

				 document.selectReleaseForm.submit();
			}

			function trim(sString)
				  {
					sTrimmedString = "";
				    if (sString != "") {
					  var iStart = 0;
					  var iEnd = sString.length - 1;
					  // var sWhitespace = " \t\f\n\r\v";
					  var sWhitespace = "   ";
		
		
					  while (sWhitespace.indexOf(sString.charAt(iStart)) != -1) {
						 iStart++;
						 if (iStart > iEnd) 
							break;
					  }

					  // If the string not just whitespace
					  if (iStart <= iEnd) {
						 while (sWhitespace.indexOf(sString.charAt(iEnd)) != -1)
							iEnd--;
							 sTrimmedString = sString.substring(iStart,++iEnd);
						  }
					   }
				   return sTrimmedString;
				 }

		</script>
	</head>

	<body>
		
		<form name="ReleaseLogHeader" method="post">
			<table width=100%>
				<tr>
					<td width=100%>
						<jsp:include page="HeaderNav_ReleaseObject.jsp" />
					</td>
				</tr>
			</table>
		</form>

		<a href="mainpage.jsp">Back to Main Page.</a>
		
		<form name="selectReleaseForm" action="ReleaseLog.jsp" method="post">
		
			<table cellpadding="0" cellspacing="1" width="60%" style="BORDER:#000000 1px solid" align='center'>
				<tr bgcolor=#EFEFEF>
					<td class=bodyblue align="center">
						<b>
							Release Name
						</b>
					</td>

					<td class=bodyblue>
						<select name=releaseName>

							 <option value='0' <% out.println("selected");%>>
								Select Release
							</option>
						
							<%
								for (int i = 0; i < relNameVector.size(); i++) {
									ReleaseVO relbean = (ReleaseVO) relNameVector.elementAt(i);

							%>

							<option value='<%=	relbean.getReleaseName()%>' <%= releaseName.equals(relbean.getReleaseName())?"selected":""%>>
								<%=relbean.getReleaseName()%>
							</option>

							<%}%>
							</select>
						</td>
						<td>
							<input type=submit class=whitebutton name=getLogButton value="Get Log" onclick="return checkForRelease(releaseName)">
						</td>
					</tr>
				</table>
			</form>
		
		<% 
			if(getLogButton.equals("Get Log")){		
		%>
			<table cellpadding="0" cellspacing="1" width="60%" style="BORDER:#000000 1px solid" align='center'>
			
				
					<% int read=0;
					 if( logFileVector!= null) {

					%>
					<tr bgcolor="#000077" >
					<td class=smallfonthead colspan=1 width="60%">
						<b>
							Release Log File Name
						</b>
					</td>

					<td class=smallfonthead colspan=1 width="20%">
						<b>
							Date
						</b>
					</td>
		
					<td class=smallfonthead colspan=1 width="20%">
						<b>
							Actions
						</b>
					</td>
					</tr>
					<%
					
							
					FileBean fileBean = new FileBean();
					for(int i=0;i<logFileVector.size();i++){
						fileBean = (FileBean)logFileVector.elementAt(i);
					%>
					<form name="ReleaseLogDisplay" action="ReleaseLog.jsp" method="post">
					<tr bgcolor=#EFEFEF>		
					<td class=bodyblue >&nbsp&nbsp&nbsp<%=fileBean.getFileName()%></td>
					<td class=bodyblue align='center'><%=fileBean.getFileDate()%></td>
					<input type='hidden' name='downloadlogfile' value='<%=fileBean.getFileName()%>'>
					<input type='hidden' name='hiddenRelName' value='<%= releaseName%>'>
					<td align='center'><input type=submit class=whitebutton name=downloadlogfilebutton  value="Get File" ></td>
					</tr>
					</form>
				<%  }
					
					}else{ %>	
						<tr bgcolor="#000077" >
							<td class=smallfonthead width="100%">
								<b>
									Release logs not Available for selected Release...
								</b>
							</td>
						</tr>	
					<%} %>
				
			</table>
		

		<% }

		%>
	</body>
</html>

<%}
			} catch (Exception e) {
				e.printStackTrace();
			}

		%>
